package com.icss.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.icss.beans.AppercetionVO;
import com.icss.beans.User;
import com.icss.daos.AppercetionDao;
import com.icss.daos.Dao;
import com.icss.utils.RecordSet;

/**
 * @author 韦锦良
 *　类的作用：学习心得servlet类
 * 最后修改时间：2010-3-13　下午03:56:35
 * 最后的改动内容：初始版本
 */
@SuppressWarnings("serial")
public class AppercetionServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("action");
		AppercetionDao dao = new AppercetionDao();
		String role = request.getParameter("role");
		String disPatcherURL = null;
		HttpSession session = request.getSession();

		//添加心得
		if ("add".equals(action)) {

			int uid = ((User) session.getAttribute("User")).getUid();
			String appercetionBody = request.getParameter("appercetionBody").trim();
			String visiable = request.getParameter("visiable");

			String appercetionTitle = request.getParameter("appercetionTitle").trim();
			String publisher = ((User) session.getAttribute("User")).getUserName();

			String sql = "insert into appercetion(uid,publisher,appercetionTitle,appercetionBody,isPublic) values("
					+ uid + ",'" + publisher + "','" + appercetionTitle + "','" + appercetionBody + "'," + visiable
					+ ")";

			boolean result = dao.execute(sql);

			PrintWriter out = response.getWriter();

			response.setContentType("text/html");

			if (result) {
				out.write("<script>alert('添加成功');window.location.href='appercetion?action=user_list&uid=" + uid
						+ "';</script>");
			} else {
				out.write("<script>alert('添加失败'window.location.href='appercetion?action=user_list&uid=" + uid
						+ "';);</script>");
			}

			out.close();

		}

		//删除心得
		if ("delete".equals(action)) {

			String appercetionID = request.getParameter("appercetionID");

			String sql = "delete from Appercetion where  appercetionID=" + appercetionID;

			boolean result = dao.execute(sql);

			PrintWriter out = response.getWriter();

			response.setContentType("text/html");
			User user = (User) session.getAttribute("User");
			int uid = user.getUid();

			if ("officeAdmin".equals(role)) {
				disPatcherURL = "appercetion?action=admin_list";
			} else {
				disPatcherURL = "appercetion?action=user_list&uid=" + uid;
			}

			if (result) {
				out.write("<script>alert('删除成功');window.location.href='" + disPatcherURL + "';</script>");
			} else {
				out.write("<script>alert('删除失败');window.location.href='" + disPatcherURL + "';</script>");
			}

			out.close();

		}

		//批量刪除心得
		if ("batchingDelete".equals(action)) {

			String[] appercetionIds = request.getParameterValues("appercetionIds");
			StringBuffer sql = new StringBuffer("delete from Appercetion where  appercetionID=");

			if (appercetionIds.length == 1) {
				sql.append(appercetionIds[0]);
			} else {

				for (int i = 0; i < appercetionIds.length; i++) {
					if (i == appercetionIds.length - 1) {
						sql.append(appercetionIds[i]);
					} else {
						sql.append(appercetionIds[i]).append(" or ").append("appercetionID=");
					}
				}
			}
			boolean result = dao.execute(sql.toString());

			PrintWriter out = response.getWriter();

			response.setContentType("text/html");
			User user = (User) session.getAttribute("User");
			int uid = user.getUid();

			if ("officeAdmin".equals(role)) {
				disPatcherURL = "appercetion?action=admin_list";
			} else {
				disPatcherURL = "appercetion?action=user_list&uid=" + uid;
			}

			if (result) {
				out.write("<script>alert('删除成功');window.location.href='" + disPatcherURL + "';</script>");
			} else {
				out.write("<script>alert('删除失败');window.location.href='" + disPatcherURL + "';</script>");
			}

			out.close();

		}

		//更新心得
		if ("update".equals(action)) {
			String appercetionID = request.getParameter("appercetionID");
			String appercetionBody = request.getParameter("appercetionBody").trim();
			String visiable = request.getParameter("visiable");

			String appercetionTitle = request.getParameter("appercetionTitle").trim();

			String sql = "update appercetion set appercetionTitle='" + appercetionTitle + "',isPublic=" + visiable
					+ ",appercetionBody='" + appercetionBody + "' where appercetionID=" + appercetionID;
			;

			boolean result = dao.execute(sql);

			PrintWriter out = response.getWriter();

			if ("officeAdmin".equals(role)) {
				disPatcherURL = "appercetion?action=admin_list";
			} else {
				int uid = ((User) request.getSession(false).getAttribute("User")).getUid();
				disPatcherURL = "appercetion?action=user_list&uid=" + uid;
			}

			response.setContentType("text/html");
			if (result) {
				out.write("<script>alert('修改成功');window.location.href='" + disPatcherURL + "';</script>");
			} else {
				out.write("<script>alert('修改失败');window.location.href='" + disPatcherURL + "';</script>");
			}

			out.close();
		}

		//普通用戶請求自己的心得
		if ("user_list".equals(action)) {

			String uid = request.getParameter("uid");
			ArrayList<AppercetionVO> vos = dao.getAllTitleByUid(uid);
			request.setAttribute("list", vos);
			request.getRequestDispatcher("/page/xinde/user_list.jsp").forward(request, response);
		}

		//管理员请求心得
		if ("admin_list".equals(action)) {

			String strCurrentPage = request.getParameter("currentPage");
			String strPageSize = request.getParameter("pageSize");
			String type = request.getParameter("type");
			String typeArg = request.getParameter("typeArg");

			int currentPage = 1;
			int pageSize = 6;

			if (null != strCurrentPage) {
				currentPage = Integer.parseInt(strCurrentPage);
			}

			if (null != strPageSize) {
				pageSize = Integer.parseInt(strPageSize);
			}
			RecordSet rs = dao.getInfoForAdmin(pageSize, currentPage, type, typeArg);
			request.setAttribute("admin_list", rs);
			request.getRequestDispatcher("/page/xinde/admin_list.jsp").forward(request, response);

		}

		//通过id获得心得的详细内容
		if ("getBodyById".equals(action)) {

			String appercetionID = request.getParameter("appercetionId");

			AppercetionVO vo = dao.getAppercetionVOById(appercetionID);

			PrintWriter out = response.getWriter();

			out.print(vo.getAppercetionBody());

			out.close();

		}

		//用户请求更新
		if ("updateRequest".equals(action)) {

			String appercetionID = request.getParameter("appercetionID");

			AppercetionVO vo = dao.getAppercetionVOById(appercetionID);

			request.setAttribute("appercetion", vo);

			JsonConfig config = new JsonConfig();
			config.setExcludes(new String[] { "publishTime" });

			JSONObject appercetion = JSONObject.fromObject(vo, config);

			PrintWriter out = response.getWriter();

			out.write(appercetion.toString());

			out.close();

		}

		//设置心得的可见性
		if ("setPublic".equals(action)) {

			String appercetionID = request.getParameter("appercetionID");

			int uid = ((User) request.getSession().getAttribute("User")).getUid();

			byte visiable = Byte.parseByte(request.getParameter("visiable"));

			String sql = "update appercetion set isPublic = " + visiable + "where appercetionID=" + appercetionID;

			boolean result = dao.execute(sql);

			PrintWriter out = response.getWriter();

			response.setContentType("text/html");

			if ("officeAdmin".equals(role)) {
				disPatcherURL = "appercetion?action=admin_list";
			} else {
				disPatcherURL = "appercetion?action=user_list&uid=" + uid;
			}

			if (result) {
				out.write("<script>alert('设置成功！');window.location.href='" + disPatcherURL + "';</script>");
			} else {
				out.write("<script>alert('设置失败！');window.location.href='appercetion?action=list&uid='" + disPatcherURL
						+ "';</script>");
			}
			out.close();
		}

		
		//前台请求心得
		if ("forward_list".equals(action)) {

			String strCurrentPage = request.getParameter("currentPage");
			String strPageSize = request.getParameter("pageSize");

			int currentPage = 1;
			int pageSize = 2;

			if (null != strCurrentPage) {
				currentPage = Integer.parseInt(strCurrentPage);
			}

			if (null != strPageSize) {
				pageSize = Integer.parseInt(strPageSize);
			}

			Dao basicDao = new Dao();

			RecordSet rs = basicDao.queryPager("select * from Appercetion where isPublic=1 order by publishTime desc",
					pageSize, currentPage);
			request.setAttribute("forward_list", rs);
			request.getRequestDispatcher("/page/xinde/forward_list.jsp").forward(request, response);
			basicDao.close();
		}

		dao.close();
	}
}
